define(['jquery','api','user','util'],function($,api,user,util){
	var vm,
	   channels=[],
	   controller = {
	   init:function(){
	   	   if(!user.checkLogin())return;
		   api.initFooter(3);
		   api.setTitle('我的银行卡');
		   if(!vm){
		     this.render();
		   }else{
		     vm.show=true;
		   }
		   this.loadChannel();
	   },
	   render:function(){
	   	   $('#pages').append('<div id="bank"></div>');
	       require(['text!pages/bank/list.html','Vue'],function(tpl,Vue){
	            vm=new Vue({
	               el:'#bank',
	               data:{
	               	  show:true,
	               	  channels:channels,
		              channelId:'',
		              pwd:'',
		              showPanel:false
	               },
	               methods:{
	                  selectChannel:function(channelId){
	              		this.channelId=channelId;
	              	},
	              	showUnbindPanel:function(){
	              		this.showPanel=true;
	              	},
	              	cancelPanel:function(){
	              		this.showPanel=false;
	              		this.pwd = '';
	              	},
	              	doUnbind:function(){
	              		var data={
	              		   channelid:this.channelId,
	              		   moneypwd:this.pwd
	              		};
	              		if (this.pwd == '') {
			                util.msg('输入支付密码！');
			                $("#pwd").focus();
			                return false;
			            }

	              		api.unBind(data,function(rs){
	              		    if(rs.retcode=='0'){
	              		        util.msg('解绑成功！');
	              		        vm.cancelPanel();
	              		    }else if(rs.retcode=='-4040'){
	              		        util.msg('密码错误！');
	              		    }else{
	              		        util.dealErrorCode(rs.retcode);
	              		    }
	              		});
	              	}
	               },
	               template:tpl
	            })
	       })
	   },
	   loadChannel:function(){
	   	   var channels=[],
	   	       channel;
	   	   api.userchannels(function(rs){
	   	       if(rs.retcode=='0'){
	   	           for(var i=0;i<rs.data.length;i++){
	   	               channel=rs.data[i];
	   	               if(channel.bankaccount!='')channels.push(channel);
	   	           }
	   	       }
	   	       if(channels.length){
		   	       vm.channelId=channels[0].channelid;
		   	       vm.channels=channels;
	   	       }else{
	   	           vm.channelId=''
	   	           vm.channels=[];
	   	       }
	   	   });
	   },
	   onRouteChange:function(){
	       if(vm)vm.show=false;
	   }
	};
    return controller;
})